package Entity;
 

 
import java.util.Date;
 

 
import Controller.Client;
 

 
/**
 
 * this is an entity for one time customer order. the customer will insert his id, the leaving hour and enering hour, his car number and mail.
 
 * the system will write his actual leaving hour and the payment status.
 
 * @author Or marx
 
 *
 
 */
 
public class OneTimeCostumerOrder {
 
        private String costumer_id;
 
        private Date leaving_hour;
 
        private Date entering_hour;
 
        private Date actual_leaving_hour;
 
        private String mail;
 
        private String car_number;
 
        private boolean payment_status;
 
        private float payment;
 
        /**
 
         * is an enum for checking the validation. it can be one of the foloowing:
 
         * CORRECT= if everything is ok.
 
         * ID_ERROR= if the id length <9.
 
         * CAR_NUMBER_ERROR= if the car number length <7.
 
         * EMAIL_ERROR= if the pattern is wrong.
 
         * LEAVING_HOUR_ERROR= if the leaving hour is wrong.
 
         *
 
         */
 
        public enum oneTimeCostumerEnum {CORRECT,ID_ERROR,CAR_NUMBER_ERROR,EMAIL_ERROR,LEAVING_HOUR_ERROR};
 
        
 
        /**
 
         * is the default constructor. it will set the payment_status to false, the entering hour to his default and the actual_leaving_hour to null.
 
         */
 
        public OneTimeCostumerOrder() {
 
                payment_status=false;
 
                this.entering_hour=new Date();
 
                this.actual_leaving_hour=null;
 
        }
 
        
 
        public OneTimeCostumerOrder(String cos,Date lev,Date en,Date ac,String ma,String ca,boolean pa,float pay) 
 
        {
 
                this.costumer_id=new String(cos);
 
                this.leaving_hour=new Date(lev.getTime());
 
                this.entering_hour=new Date(en.getTime());
 
                this.actual_leaving_hour=null;
 
                this.mail=new String(ma);
 
                this.car_number=new String(ca);
 
                this.payment_status=pa;
 
                this.payment=pay;
 
        }
 
        /**
 
         * is the other constructor. it will set a new order by the following parameters:
 
         * @param costumer_id
 
         * @param leaving_hour
 
         * @param mail
 
         * @param car_number
 
         */
 
        public OneTimeCostumerOrder(String costumer_id, Date leaving_hour, String mail, String car_number) 
 
        {
 
                payment_status=false;
 
                this.costumer_id=costumer_id;
 
                this.leaving_hour=(Date) leaving_hour.clone();
 
                this.mail=mail;
 
                this.car_number=car_number;
 
                this.entering_hour=new Date();
 
                this.actual_leaving_hour=null;
 
        }
 
        
 
        /**
 
         * will save the order to the DB.
 
         */
 
        public void saveToDB()
 
        {
 
                Client.saveOneTimeCostumerToDB(this);
 
        }
 
        
 
        /**
 
         * is the validation function. will check the length of the customer id and the car number, the mail pattern and the leaving hour.
 
         * @return
 
         */
 
        public oneTimeCostumerEnum validate()
 
        {
 
                if (this.costumer_id.length() != 9)
 
                        return oneTimeCostumerEnum.ID_ERROR;
 
                try{
 
                          Integer.parseInt(this.costumer_id);
 
                          // is an integer!
 
                        } catch (NumberFormatException e) {
 
                                return oneTimeCostumerEnum.ID_ERROR;// not an integer!
 
                        }
 
                
 
                if (this.car_number.length() != 7)
 
                        return oneTimeCostumerEnum.CAR_NUMBER_ERROR;
 
                try{
 
                          Integer.parseInt(this.car_number);
 
                          // is an integer!
 
                        } catch (NumberFormatException e) {
 
                                return oneTimeCostumerEnum.CAR_NUMBER_ERROR;// not an integer!
 
                        }
 
                if(this.leaving_hour.compareTo(new Date())<=0)
 
                        return oneTimeCostumerEnum.LEAVING_HOUR_ERROR;
 
                if(mail.indexOf('@')==-1 || mail.lastIndexOf('.')<mail.indexOf('@') || mail.startsWith("@"))
 
                        return oneTimeCostumerEnum.EMAIL_ERROR;
 
                
 
                return oneTimeCostumerEnum.CORRECT;     
 
        }
 
        
 
        
 
        public String getCostumer_id() {
 
                // TODO Auto-generated method stub
 
                return costumer_id;
 
        }
 
        public boolean isPayment_status() {
 
                return payment_status;
 
        }
 
        public void setPayment_status(boolean payment_status) {
 
                this.payment_status = payment_status;
 
                Client.updateOneTimeCustomerOrder(this);
 
        }
 
        public Date getLeaving_hour() {
 
                return leaving_hour;
 
        }
 
        public void setLeaving_hour(Date leaving_hour) {
 
                this.leaving_hour = leaving_hour;
 
                Client.updateOneTimeCustomerOrder(this);
 
        }
 
        public String getMail() {
 
                return mail;
 
        }
 
        public void setMail(String mail) {
 
                this.mail = mail;
 
                Client.updateOneTimeCustomerOrder(this);
 
        }
 
        public String getCar_number() {
 
                return car_number;
 
        }
 
        public void setCar_number(String car_number) {
 
                this.car_number = car_number;
 
                Client.updateOneTimeCustomerOrder(this);
 
        }
 
        public String toString()
 
        {
 
                return "costumer id: " + costumer_id + "\nleaving time: " + leaving_hour + "\nmail: " + mail + "\ncar number: " + car_number;
 
        }
 

 
        public Date getEntering_hour() {
 
                return entering_hour;
 
        }
 

 
        public void setEntering_hour(Date entering_hour) {
 
                this.entering_hour = entering_hour;
 
                Client.updateOneTimeCustomerOrder(this);
 
        }
 

 
        public Date getActual_leaving_hour() {
 
                return actual_leaving_hour;
 
        }
 

 
        public void setActual_leaving_hour(Date actual_leaving_hour) {
 
                this.actual_leaving_hour = actual_leaving_hour;
 
        }
 

 
        public float getPayment() {
 
                return payment;
 
        }
 

 
        public void setPayment(float payment) {
 
                this.payment = payment;
 
        }
 

 
} 
